﻿using System;
using System.Data;
using System.Data.SqlClient;
using Dtl;
using System.Collections.Generic; 
using System.Collections;

namespace Dal
{
    public class EmailGroupDao
    {

        /*LISTADO DE GRUPOS DE EMAILS*/
        public static DataSet getEmailGroups()
        {

            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_GetEmailGroups";            
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtEmailGroups");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getEmailGroups: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsEmailGroups");
            ds.Tables.Add(dt);
            return ds;
        }

        public static DataSet getEmailGroupsItems(int idEmailGroup)
        {

            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_GetEmailGroupsItems";
            cmd.Parameters.AddWithValue("@idEmailGroup", idEmailGroup);
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtEmailGroupItems");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getEmailGroupsItems: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsEmailGroupItems");
            ds.Tables.Add(dt);
            return ds;
        }


        public static int setEmailGroup(List<EmailGroup> listEmailGroup, List<EmailGroupItem> listEmailGroupItem) 
        {
            int insert = 0;

            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }

            SqlTransaction tr = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);

            try
            {
                cmd.Connection.Open();
                cmd.Connection.CreateCommand();
                tr = cmd.Connection.BeginTransaction("Transaction");
                cmd.Transaction = tr;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                foreach(Dtl.EmailGroup emailGroup in listEmailGroup)
                {
                    if (emailGroup.typeOperation == true) //GUARDAR UN GRUPO
                    {
                        cmd.CommandText = "dbo.dms_Set_EmailGroups";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@DsName", emailGroup.dsName);                       

                    }
                    else //MODIFICA UN GRUPO
                    {
                        cmd.CommandText = "dbo.dms_Set_EmailGroups_Change";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@IdEmailGroup", emailGroup.idEmailGroup);
                        cmd.Parameters.AddWithValue("@DsName", emailGroup.dsName);   
                    
                    }

                    insert = cmd.ExecuteNonQuery();
                
                }

                foreach (Dtl.EmailGroupItem emailGroupItem in listEmailGroupItem)
                {
                    if (emailGroupItem.typeOperation == true) //GUARDAR UN ITEM DEL GRUPO
                    {
                        cmd.CommandText = "dbo.dms_Set_EmailGroupsItems";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@idEmailGroup", emailGroupItem.idEmailGroup);
                        cmd.Parameters.AddWithValue("@idDistributor", emailGroupItem.idDistributor);
                    }

                    insert = cmd.ExecuteNonQuery();
                }

                tr.Commit();
                cmd.Connection.Close();
                return insert;
            }
            catch (Exception exc)
            {
                tr.Rollback();
                cmd.Connection.Close();
                Credentials.writeLogFile("setEmailGroup+: " + exc.Message);
                return -2;
            }
        
        }

        public static int DeleteEmailGroup(int idEmailGroup)
        {
            int delete = 0;
            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Delete_EmailGroups";
            cmd.Parameters.AddWithValue("@IdEmailGroup", idEmailGroup);


            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                delete = 1;
                return delete;
            }
            catch (SqlException exc)
            {

                Credentials.writeLogFile("DeleteEmailGroup: " + exc.Message);
                return 0;
            }


        }

        public static int DeleteEmailGroupItem(int idEmailGroup, int idDistributor)
        {
            int delete = 0;
            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Delete_EmailGroupsItems";
            cmd.Parameters.AddWithValue("@idEmailGroup", idEmailGroup);
            cmd.Parameters.AddWithValue("@idDistributor", idDistributor);


            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                delete = 1;
                return delete;
            }
            catch (SqlException exc)
            {

                Credentials.writeLogFile("DeleteEmailGroupItem: " + exc.Message);
                return 0;
            }


        }


    }
}
